package com.gas.gm.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.gas.gm.dao.AccountDao;
import com.gas.gm.model.Account;
import com.gas.gm.util.GasContextHelper;
import com.gas.gm.util.converter.xml.AccountXMLFileConverter;
import com.gas.gm.util.converter.xml.XMLFileConverter;

@Service("accountService")
public class IAccountService implements AccountService {
	
	private AccountDao accountDao;
	
	private XMLFileConverter<Account> converter;
	
	public IAccountService() {
	
	}

	public void createAccount(Account a) {
		getAccountDao().persist(a);
		updateXML();
	}


	public void removeAccount(Account a) {
		getAccountDao().remove(a);
		updateXML();
	}

	public Account findAccount(String id) {
		return getAccountDao().findAccount(id);
	}

	public List<Account> findAllAccounts() {
		return getAccountDao().findAllAccounts();
	}
	
	public Account updateAccount(Account a) {
		Account merged=getAccountDao().merge(a);
		updateXML();
		return merged;
	}

	
	AccountDao getAccountDao()
	{
		if(accountDao==null){
			accountDao=(AccountDao) GasContextHelper.getApplicationContext().getBean(AccountDao.class);
		}
		return accountDao;
		
	}
	
	XMLFileConverter<Account> getConverter(){
		if(converter==null){
			converter=new AccountXMLFileConverter();
		}
		return converter;
	}
	
	void updateXML(){
		getConverter().parseToXML();
	}
	
	Account parseFromXMLFile(String id){
		return getConverter().composeFromXML(id);
	}

	public Account findFromFile(String id) {
		return parseFromXMLFile(id);
	}

	
}
